import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
	JPanel my_panel;
	JButton my_button;
	JTextField my_text1;
	JLabel my_label1;
	JComponent my_border;
	JButton button1;
	JButton button2;
	JButton button3;
	JButton button4;
	JButton button5;
	JButton button6;
	JButton button7;
	JButton button8;
	JButton button9;
	JButton button0;
	JButton button10;
	JButton button11;     
	JButton button12;
	JButton button13;
	JButton button14;
	JButton button15;
	
	String opp = "";
	double num1;
	double num2;
	
	
	

	public Calculator(){
		setTitle("calculator");
		setSize(600,400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setUpPanel();
		this.add(my_panel);
		this.pack();
		
	}
	
	public void setUpPanel(){
		
		setLayout(new GridLayout(7,7));
		button1 = new JButton("1");
		button1.addActionListener(this);
		add(button1);
		
		button2 = new JButton("2");
		button2.addActionListener(this);
		add(button2);
		
		button3 = new JButton("3");
		button3.addActionListener(this);
		add(button3);
		
		button4 = new JButton("4");
		button4.addActionListener(this);
		add(button4);
	
		button5 = new JButton("5");
		button5.addActionListener(this);
		add(button5);
		
		button6 = new JButton("6");
		button6.addActionListener(this);
		add(button6);
		
		button7 = new JButton("7");
		button7.addActionListener(this);
		add(button7);
		
		button8 = new JButton("8");
		button8.addActionListener(this);
		add(button8);
		
		button9 = new JButton("9");
		button9.addActionListener(this);
		add(button9);
		
		button0 = new JButton("0");
		button0.addActionListener(this);
		add(button0);
	
		button10 = new JButton("*");
		button10.addActionListener(this);
		add(button10);
		
		button11 = new JButton("+");
		button11.addActionListener(this);
		add(button0);
		
		button12 = new JButton("-");
		button12.addActionListener(this);
		add(button12);
		
		button13 = new JButton("/");
		button13.addActionListener(this);
		add(button13);
		
		button14 = new JButton("=");
		button14.addActionListener(this);
		add(button14);
		
		button15 = new JButton("Clear");
		button15.addActionListener(this);
		add(button15);
		
		BorderLayout layout = new BorderLayout();
		my_panel = new JPanel();
		my_panel.setLayout(layout);
		my_label1 = new JLabel("number1: "); // asking for the first number
		my_panel.add(my_label1,layout.CENTER);
		
		
		my_panel.setLayout(new BoxLayout(my_panel, BoxLayout.Y_AXIS));
		
		my_text1 = new JTextField(10);
		JPanel textpanel = new JPanel();
		
		textpanel.add(my_text1);
		my_panel.add(textpanel);
		
		GridLayout numberLayout = new GridLayout();
			
		
		
	}
	
		
		
	
	public static void main(String[] args) {
		Calculator calculator = new Calculator();
		calculator.setVisible(true);
	
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
			Object source = e.getSource();	
			System.out.println(source);
				if(source == button1){
					String curr = my_text1.getText();
					curr +="1";
					my_text1.setText(curr);
				    }
					else if (source == button2){
						String curr = my_text1.getText();
						curr +="2";
						my_text1.setText(curr);
					}
					else if (source == button3){
						String curr = my_text1.getText();
						curr +="3";
						my_text1.setText(curr);
					}
					else if (source == button4){
						String curr = my_text1.getText();
						curr +="4";
						my_text1.setText(curr);
					}
					else if (source == button5){
						String curr = my_text1.getText();
						curr +="5";
						my_text1.setText(curr);
					}
					else if (source == button6){
						String curr = my_text1.getText();
						curr +="6";
						my_text1.setText(curr);
					}
				
					else if (source == button7){
						String curr = my_text1.getText();
						curr +="7";
						my_text1.setText(curr);
					}
					else if (source == button8){
						String curr = my_text1.getText();
						curr +="8";
						my_text1.setText(curr);
					}
					else if (source == button9){
						String curr = my_text1.getText();
						curr +="9";
						my_text1.setText(curr);
					}
					else if (source == button0){
						String curr = my_text1.getText();
						curr +="0";
						my_text1.setText(curr);
					}
					else if (source == button10){
						String curr = my_text1.getText();
						num1=Double.parseDouble(curr);
						my_text1.setText("");
						opp = "*";
					}
					else if (source == button11){
						String curr = my_text1.getText();
						num1=Double.parseDouble(curr);
						my_text1.setText("");
						opp = "+";
					}
					else if (source == button12){
						String curr = my_text1.getText();
						num1=Double.parseDouble(curr);
						my_text1.setText("");
						opp = "-";
					}
					else if (source == button13){
						String curr = my_text1.getText();
						num1=Double.parseDouble(curr);
						my_text1.setText("");
						opp = "/";
					}
					else if (source == button14){
						num2= Double.parseDouble(my_text1.getText());
						
						String curr = my_text1.getText();
						curr += "=";
						my_text1.setText(curr);
						if(opp.equals("*")){
							Double opp = num1*num2;
							my_text1.setText(opp.toString());
							
						}
						else if(opp.equals("+")){
							Double opp = num1+num2;
							my_text1.setText(opp.toString());
						}
						else if(opp.equals("-")){
							Double opp = num1-num2;
							my_text1.setText(opp.toString());
						}
						else if(opp.equals("/")){
							Double opp = num1/num2;
							my_text1.setText(opp.toString());
						
						}
						
						
					}
					else if (source == button15){
						String curr = "";
						my_text1.setText("");
						
						
						
					}
		
						
	}


}

